#！/usr/bin/env python
#!coding=utf-8
import sys
import os
import _thread as thread
import time

def thread_entry(id):
    cnt = 0
    while cnt < 10:          # 循环次数不够
        print('Thread:(%d) Time:%s cnt=%d' % (id, time.ctime(), cnt))
        time.sleep(1)               # 休息一秒
        cnt = cnt + 1
g_continue = True 
def signout_entry(id):
    global g_continue
    while True:
        if not g_continue:           # 如果标志为False，退出
            print("Thread:(%d) exit" % id)
            thread.exit_thread()     # 退出线程
        else:
            print('Thread:(%d) Time:%s' % (id, time.ctime()))
            time.sleep(2)

def start_threads():                  # 启动线程
    t1 = thread.start_new_thread(thread_entry, (1,))
    t2 = thread.start_new_thread(thread_entry, (2,))
    time.sleep(12)

def start_out_threads():                  # 启动线程
    global g_continue  #必须要申明global 否则相当于下面的赋值相当于重新创建的了一个变量
    t1 = thread.start_new_thread(signout_entry, (3,))
    t2 = thread.start_new_thread(signout_entry, (4,))
    time.sleep(1)
    g_continue = False
    time.sleep(50)

def run():
    #start_threads()
    start_out_threads()

if __name__ == "__main__":
    run()